    var forum    = null;
    var count    = null;
    var offset   = 0;
    var answers  = 0;

    $(document).ready(function(){
        initParam();
        setStyle("messages");
        
        $("#a_head_l").hide();
             
        $.mobile.loading('show');
        var http = "http://anibox.me/api.php?task=get_forum_topic&topic="+forum+"&offset=0&count=1";
        $.getJSON(http, setDiscription);
        var http = "http://anibox.me/api.php?task=get_forum_topic&topic="+forum+"&offset="+offset+"&count="+count;
        $.getJSON(http, parseJson);
    }); 
    
    //initialization params
    function initParam(){
        forum = getParam("id");   
        answers = getParam("answers");
        count = getListCount();
        
        if (parseInt(answers) > parseInt(count) ){
            offset = parseInt(answers) - parseInt(count);
        }
        
    }
    
    function setDiscription(data){
        var buf =  
            '<div class="div_style_update">' +
            data.messages[0].message +'</div>'+
            '<p>Автор: <font color="red">' +  data.messages[0].author + '</font>'+
            ' Дата: ' + parseUnixTimeStamp(data.messages[0].date) + '</p>';
        
        $('#description').append(buf);
        
    }
    
    //parse data in list
    function parseJson(data){
        if (data.messages.length == 0){
            offset = parseInt(offset) - parseInt(count);
            $('#a_head_r').hide();
            var http = "http://anibox.me/api.php?task=get_forum_topic&topic="+forum+"&offset="+offset+"&count="+count;
            $.getJSON(http, parseJson);
            return;
        }
        
        
        if(data.messages.length < count){
            $('#a_head_r').hide();
        }
        
        document.getElementById("p_head").innerHTML = data.title;
        var size = 0;
        if( offset == 0)
            size = 1;
            
        for (var i=data.messages.length-1; i>=size; i--) {
            $('#list').append(
                '<li>' +
                
                
                '<div class="div_style_update">' +
                 data.messages[i].message +
                 '</div>' +
                 '<p>Автор: <font color="red">' +  data.messages[i].author + '</font>'+
                 ' Дата: ' + parseUnixTimeStamp(data.messages[i].date) + '</p>'+
                 
                '</li>\n'
            );
        }
        $('#list').listview('refresh');
        $.mobile.loading('hide');
    }
    
    //click in head befo
    function updateBefo(){
        var int;
        if (parseInt(offset) < (parseInt(answers)-count) ){
            int = parseInt(offset) + parseInt(count);
        }else{
            $("#a_head_l").hide();
            return;
        }  
        $("#a_head_r").show();
        $('#list').empty();
        offset = int;
        var http = "http://anibox.me/api.php?task=get_forum_topic&topic="+forum+"&offset="+offset+"&count="+count;
        $.mobile.loading('show');
        $.getJSON(http, parseJson); 
        window.scrollTo(0, 0);
    }
    
    //click in head next
    function updateNext(){
        var int;
        if (parseInt(offset) > parseInt(count) ){
            int = parseInt(offset) - parseInt(count);
        }else{
            $('#a_head_r').hide();
            if (int != 0){
                int = 0;
            }else{
                return;
            }
        }
        $('#a_head_l').show();
        $('#list').empty();
        offset   = int;
        var http = "http://anibox.me/api.php?task=get_forum_topic&topic="+forum+"&offset="+offset+"&count="+count;
        $.mobile.loading('show');
        $.getJSON(http, parseJson);
        window.scrollTo(0, 0);
    }
    
    function butPress(){
    	var mess = document.getElementById("message").value;
    	if (mess == ''){
    		alert('Введите сообщение,');
    	}
    
    	var log = window.localStorage.getItem("login");
    	if (log == null){
    		var log = window.sessionStorage.getItem("login");
    		if(log == null){
    			alert('вы не авторизованы');
    			return;
    		}else{
    			var pas = window.sessionStorage.getItem("password");
    		}
    	}else{
			var pas = window.localStorage.getItem("password");
    	}	
    	
    	    
    		var http = "http://anibox.me/api.php?task=add_forum_message&name="+log+"&password="+pas+"&topic_id="+forum+"&message="+mess;
        	$.getJSON(http, sendJSON);
    }
    
    function sendJSON(data){
    	location.reload();  
    }
    